package main.java.indi.zyj.sword;

public class MyAtoi {

    public static void main(String[] args) {
        System.out.println(Integer.MAX_VALUE);
    }

    public int myAtoi(String str) {

        char[] c = str.trim().toCharArray();

        if(c.length == 0) return 0;

        int res = 0, boundry = Integer.MAX_VALUE / 10;
        int start = 1, sign = 1;

        if(c[0] == '-') {
            sign = -1;
        } else if(c[0] != '+') {
            start = 0;
        }

        for(int i = start; i < c.length; i++) {

            if(c[i] < '0' || c[i] > '9') break;

            // >2147483647
            if(res > boundry || res == boundry && c[i] > '7')
                return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;

            res = res * 10 + (c[i] - '0');
        }
        return sign * res;
    }

}
